package mission_tour.solution;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

public class service_dialog_call extends Activity implements ServerIp{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        AlertDialog.Builder event_call = new AlertDialog.Builder(this)
		.setTitle(items.appname)
		.setMessage(items.event_name+" 에서\n미션이 발견되었습니다! 플레이 하시겠습니까?")
		.setIcon(R.drawable.icon)
		.setPositiveButton("예", new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				items.event_called = true;
				try {
					items.event_list.removeAll(items.event_list);
					
					URL url = null;
			        String resultString =null;
			  
		        	url = new URL(tour_event_call+"?app_type="+items.apptype+"&event_position="+items.event_position);
				
		        	HttpURLConnection http = (HttpURLConnection)url.openConnection();  
			
		        	BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));  
		        	StringBuffer buffer = new StringBuffer();
					 
		        	int z;
		        	
					while((z=in.read()) != -1){
						buffer.append((char)z);
					}
					
					resultString= buffer.toString().trim();
					
		            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		            factory.setNamespaceAware(true);
		            
		            boolean tagok = false;
		            
		            String[] quiz_call = null;
		            
		            XmlPullParser xpp = factory.newPullParser();
		            xpp.setInput(new StringReader(resultString));
		            
		            int eventType = xpp.getEventType();
		            
		            while (eventType != XmlPullParser.END_DOCUMENT) {
		                
		            	if(eventType == XmlPullParser.START_DOCUMENT) {
		                    System.out.println("Start document");
		                }
		            	
		            	if (eventType == XmlPullParser.END_DOCUMENT) {
		                    System.out.println("End document");
		                }
		            	
		            	if (eventType == XmlPullParser.START_TAG) {
		            		
		            		if(xpp.getName().equals("name")){
		            			tagok = true;
		            		}
		                }
		            	
		            	if (eventType == XmlPullParser.END_TAG) {
		                }
		            	
		            	if (eventType == XmlPullParser.TEXT) {
		            		if(tagok){
		            			quiz_call = xpp.getText().split(",");
		                	}
		            		quizs qs = new quizs(quiz_call[0],quiz_call[1],quiz_call[2],quiz_call[3],quiz_call[4],quiz_call[5],quiz_call[6],Integer.parseInt(quiz_call[7]),Integer.parseInt(quiz_call[8]),quiz_call[9],Integer.parseInt(quiz_call[10]));
		            
		            		items.event_list.add(qs);
		                }
		                eventType = xpp.next();
		            }
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}catch(Exception e){
					System.out.println(e.toString());
				}
				if(items.event_type == 1){
					Intent intent = new Intent(service_dialog_call.this, tour_oxquiz.class);
					startActivityForResult(intent, 0);
					finish();
				}else if(items.event_type == 3){
					Intent intent = new Intent(service_dialog_call.this, tour_error_picture_search.class);
					startActivityForResult(intent, 0);
					finish();
				}
			}
				
		})
        .setNegativeButton("아니오", new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        event_call.show();
    }
}
